package org.jeecg.cn.edu.scau.vm.machineTime.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.cn.edu.scau.vm.common.consts.DefaultConst;
import org.jeecg.cn.edu.scau.vm.machineTime.entity.VmGroupSettings;
import org.jeecg.cn.edu.scau.vm.machineTime.mapper.VmGroupSettingsMapper;
import org.jeecg.cn.edu.scau.vm.machineTime.service.IVmGroupSettingsService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class VmGroupSettingsServiceImpl
        extends ServiceImpl<VmGroupSettingsMapper, VmGroupSettings>
        implements IVmGroupSettingsService {

    @Resource
    private VmGroupSettingsMapper groupSettingsMapper;
    @Resource
    private ISysDepartService departService;


    @Override
    public VmGroupSettings createGroupSettings(String orgCode) {
        // 1. 获取分组相关信息
        QueryWrapper<SysDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_code", orgCode);
        SysDepart depart = departService.getOne(queryWrapper);

        VmGroupSettings groupSettings = VmGroupSettings.builder()
                .createBy("admin")
                .createTime(LocalDateTime.now())
                .updateBy("admin")
                .updateTime(LocalDateTime.now())
                .sysOrgCode(depart.getOrgCode())
                .sysOrgName(depart.getDepartName())
                .gpuCardNum(DefaultConst.DEFAULT_GPU_CARD_NUMBER)
                .machineTimeAllocate(DefaultConst.DEFAULT_MACHINE_TIME_PER_MONTH)
                .build();
        groupSettingsMapper.insert(groupSettings);
        return groupSettings;
    }

    @Override
    public VmGroupSettings getByOrgCode(String orgCode) {
        return groupSettingsMapper.selectByOrgCode(orgCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int syncGroupData() {
        int addCount = 0;
        // 1. 过滤出本地表中含有的departCode
        List<VmGroupSettings> groupSettingsList = this.list();
        List<SysDepart> departs = Collections.emptyList();
        List<String> orgCodeList = groupSettingsList.stream().map(VmGroupSettings::getSysOrgCode).collect(Collectors.toList());
        if (orgCodeList.isEmpty()) {
            departs = departService.list();
        } else {
            QueryWrapper<SysDepart> queryWrapper = new QueryWrapper<>();
            queryWrapper.notIn("org_code", orgCodeList);
            departs = departService.list(queryWrapper);
        }
        // 2. 获取到差集部门后,执行插入
        List<VmGroupSettings> toBeAdd = departs.stream().map((depart) -> VmGroupSettings.builder()
                .sysOrgCode(depart.getOrgCode())
                .createBy("admin")
                .createTime(LocalDateTime.now())
                .updateBy("admin")
                .updateTime(LocalDateTime.now())
                .sysOrgName(depart.getDepartName())
                .sysOrgCode(depart.getOrgCode())
                .machineTimeAllocate(DefaultConst.DEFAULT_MACHINE_TIME_PER_MONTH)
                .gpuCardNum(DefaultConst.DEFAULT_GPU_CARD_NUMBER)
                .build()).collect(Collectors.toList());
        addCount = toBeAdd.size();
        this.saveBatch(toBeAdd);
        return addCount;
    }
}
